//
//  BaseModel.m
//  E-Cycle(XYXMain)
//
//  Created by 象萌cc003a on 17/2/20.
//  Copyright © 2017年 xmcc003a. All rights reserved.
//

#import "BaseModel.h"
#define kMsgSuccess @"成功!"
#import <objc/runtime.h>
#import "BaseItemModel.h"
@implementation BaseModel
-(instancetype)initWithSourceDict:(NSDictionary *)dict{
    self = [super init];
    if (self) {
        id success = [dict valueForKey:@"success"];
        if ([success isKindOfClass:[NSNumber class]]) {
            _success = [success boolValue];
        }
        id msg = [dict valueForKey:@"msg"];
        self.data = dict;
        if ([msg isKindOfClass:[NSString class]]) {
            if ([msg length]>0) {
                _msg = msg;
            }
        }
        self.code = [[dict objectForKey:@"errcode"]intValue];
        if (self.code == 20000) {
            [[NSNotificationCenter defaultCenter]postNotificationName:@"NEED_LOGIN" object:nil];
        }
    }
    return self;
}

-(instancetype)initWithSourceDict:(NSDictionary *)dict listItemClass:(Class)cls{
    self = [self initWithSourceDict:dict];
    if (self) {
        [self getContent:dict withClass:cls];
    }
    return self;
}

-(void)getContentWithArray:(NSArray *)array withClass:(Class)cls{
    NSMutableArray *tmpArray = [NSMutableArray array];
    for (NSDictionary *element in array) {
        BaseItemModel *model = [[cls alloc]initWithSource:element];
        [tmpArray addObject:model];
    }
    _list = [tmpArray copy];
}

-(void)getContent:(NSDictionary *)dict withClass:(Class)cls{
    NSMutableArray *tmpArray = [NSMutableArray array];
    NSDictionary *data = [dict objectForKey:@"data"];
    if ([data isKindOfClass:[NSDictionary class]]) {
        NSArray *rows = [data objectForKey:@"rows"];
        if ([rows isKindOfClass:[NSArray class]]) {
            for (NSDictionary *element in rows) {
                BaseItemModel *model = [[cls alloc]initWithSource:element];
                [tmpArray addObject:model];
            }
        }
        _list = tmpArray;
    }else{
        [self getContentWithArray:(NSArray *)data withClass:cls];
    }
}

-(instancetype)initWithDictArr:(NSDictionary *)dict{
    self = [super init];
    if (self) {
        
    }
    return self;
}
-(NSString *)contentForKey:(NSString *)key withSource:(NSDictionary *)source{
    NSString *result = [source valueForKey:key];
    if ([result isKindOfClass:[NSString class]]) {
        return result;
    }else if ([result isKindOfClass:[NSNumber class]]){
        CGFloat value = [result floatValue];
        return [NSString stringWithFormat:@"%.1f",value];
    }else{
        return @"";
    }
}

-(CGFloat)floatContentForKey:(NSString *)key withSource:(NSDictionary *)source{
    id result = [source valueForKey:key];
    if ([result isKindOfClass:[NSNumber class]]) {
        return [result floatValue];
    }
    return 0.0;
}

-(NSString *)intContentForKey:(NSString *)key withSource:(NSDictionary *)source{
    NSString *result = [source valueForKey:key];
    if ([result isKindOfClass:[NSString class]]) {
        return result;
    }else if ([result isKindOfClass:[NSNumber class]]){
        int value = [result intValue];
        return [NSString stringWithFormat:@"%i",value];
    }else{
        return @"-1";
    }
}
@end
